package com.changjie.wang.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsClient_UpdateDoc {
    /**
     * 修改文档数据
     * @param args
     */
    public static void main(String[] args) {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        try {
            //修改数据
            UpdateRequest request = new UpdateRequest();
            request.index("user").id("001");
            //指定内容类型
            request.doc(XContentType.JSON,"name","张三");
            //发送修改文档请求
            UpdateResponse response = esClient.update(request,RequestOptions.DEFAULT);
            System.out.println(response.getResult());
            System.out.println(response.getIndex());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭ES客户端
            try {
                esClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
